package com.yonyou.brigade.third_parties.fanscar.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.brigade.third_parties.fanscar.constants.FanscarConstants;
import com.yonyou.brigade.third_parties.fanscar.model.FanscarConfig;
import com.yonyou.brigade.third_parties.fanscar.model.FanscarJobGrade;
import com.yonyou.brigade.third_parties.fanscar.model.FanscarUser;
import com.yonyou.brigade.third_parties.fanscar.service.IFanscarDocService;
import com.yonyou.brigade.third_parties.fanscar.service.IFanscarPublicService;
import com.yonyou.brigade.util.HttpClientUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 泛嘉部门信息维护 服务实现类
 *
 * @author: kangfw5
 * @since : 2019-03-01
 */
@Service
public class FanscarDocServiceImpl implements IFanscarDocService {
    private final static Logger logger = LoggerFactory.getLogger(FanscarDocServiceImpl.class);

    @Value("${fanscar.apiUrl}")
    private String apiUrl;

    @Autowired
    private IFanscarPublicService fanscarPublicService;

    /**
     * 同步部门信息
     *
     * @param departmentList 部门列表
     * @param fanscarConfig  认证配置信息
     * @since : 2019-03-01
     */
    @Override
    public JSONArray createDepartment(List<Map<String, String>> departmentList, FanscarConfig fanscarConfig) {
        String token = fanscarPublicService.getToken(fanscarConfig);
        Map<String, String> headers = new HashMap<>(1);
        headers.put("Authorization", "bearer " + token);
        Map<String, List> params = new HashMap<>(1);
        params.put("Departments", departmentList);
        logger.info("部门创建开始，同步内容" + departmentList);
        String result = HttpClientUtil.post(apiUrl + FanscarConstants.CREATE_DEPARTMENT_URI, headers, null, JSON.toJSONString(params));
        logger.info("部门创建结束，同步结果" + result);

        JSONArray jsonArray = JSONObject.parseObject(result).getJSONArray("data");
        return jsonArray;
    }

    /**
     * 更新部门信息
     *
     * @param departmentList 部门列表
     * @param fanscarConfig  认证配置信息
     * @since : 2019-03-01
     */
    @Override
    public JSONArray updateDepartment(List<Map<String, String>> departmentList, FanscarConfig fanscarConfig) {
        String token = fanscarPublicService.getToken(fanscarConfig);
        Map<String, String> headers = new HashMap<>(1);
        headers.put("Authorization", "bearer " + token);
        Map<String, List> params = new HashMap<>(1);
        params.put("Departments", departmentList);
        logger.info("部门更新开始，同步内容" + departmentList);
        String result = HttpClientUtil.post(apiUrl + FanscarConstants.UPDATE_DEPARTMENT_URI, headers, null, JSON.toJSONString(params));
        logger.info("部门更新结束，同步结果" + result);

        JSONArray jsonArray = JSONObject.parseObject(result).getJSONArray("data");
        return jsonArray;
    }

    /**
     * 创建职级信息
     *
     * @param jobGradeList  职级列表
     * @param fanscarConfig 认证配置信息
     * @since : 2019-03-01
     */
    @Override
    public JSONArray createJobGrade(List<FanscarJobGrade> jobGradeList, FanscarConfig fanscarConfig) {
        String token = fanscarPublicService.getToken(fanscarConfig);
        Map<String, String> headers = new HashMap<>(1);
        headers.put("Authorization", "bearer " + token);
        Map<String, List> params = new HashMap<>(1);
        params.put("JobGrades", jobGradeList);
        logger.info("职级创建开始，同步内容" + jobGradeList);
        String result = HttpClientUtil.post(apiUrl + FanscarConstants.CREATE_JOB_GRADE_URI, headers, null, JSON.toJSONString(params));
        logger.info("职级创建结束，同步结果" + result);

        JSONArray jsonArray = JSONObject.parseObject(result).getJSONArray("data");
        return jsonArray;
    }

    /**
     * 更新职级信息
     *
     * @param jobGradeList  职级列表
     * @param fanscarConfig 认证配置信息
     * @since : 2019-03-01
     */
    @Override
    public JSONArray updateJobGrade(List<FanscarJobGrade> jobGradeList, FanscarConfig fanscarConfig) {
        String token = fanscarPublicService.getToken(fanscarConfig);
        Map<String, String> headers = new HashMap<>(1);
        headers.put("Authorization", "bearer " + token);
        Map<String, List> params = new HashMap<>(1);
        params.put("JobGrades", jobGradeList);
        logger.info("职级更新开始，同步内容" + jobGradeList);
        String result = HttpClientUtil.post(apiUrl + FanscarConstants.UPDATE_JOB_GRADE_URI, headers, null, JSON.toJSONString(params));
        logger.info("职级更新结束，同步结果" + result);

        JSONArray jsonArray = JSONObject.parseObject(result).getJSONArray("data");
        return jsonArray;
    }

    /**
     * 批量创建员工信息
     *
     * @param employeeList  员工信息列表
     * @param fanscarConfig 认证配置信息
     * @since : 2019-03-05
     */
    @Override
    public JSONArray createEmployee(List<FanscarUser> employeeList, FanscarConfig fanscarConfig) {
        String token = fanscarPublicService.getToken(fanscarConfig);
        Map<String, String> headers = new HashMap<>(1);
        headers.put("Authorization", "bearer " + token);
        Map<String, List> params = new HashMap<>(1);
        params.put("Employees", employeeList);
        logger.info("员工创建开始，同步内容" + employeeList);
        String result = HttpClientUtil.post(apiUrl + FanscarConstants.CREATE_EMPLOYEE_URI, headers, null, JSON.toJSONString(params));
        logger.info("员工创建结束，同步结果" + result);

        JSONArray jsonArray = JSONObject.parseObject(result).getJSONArray("data");
        return jsonArray;

    }

    /**
     * 批量更新员工信息
     *
     * @param employeeList  员工信息列表
     * @param fanscarConfig 认证配置信息
     * @since : 2019-03-05
     */
    @Override
    public JSONArray updateEmployee(List<FanscarUser> employeeList, FanscarConfig fanscarConfig) {
        String token = fanscarPublicService.getToken(fanscarConfig);
        Map<String, String> headers = new HashMap<>(1);
        headers.put("Authorization", "bearer " + token);
        Map<String, List> params = new HashMap<>(1);
        params.put("Employees", employeeList);
        logger.info("员工更新开始，同步内容" + employeeList);
        String result = HttpClientUtil.post(apiUrl + FanscarConstants.UPDATE_EMPLOYEE_URI, headers, null, JSON.toJSONString(params));
        logger.info("员工更新结束，同步结果" + result);

        JSONArray jsonArray = JSONObject.parseObject(result).getJSONArray("data");
        return jsonArray;
    }
}
